package beautiful.butterfly.distributed_task_processing_engine.user_definition;

import beautiful.butterfly.distributed_task_processing_engine.tools.Group;
import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的生活为什么要有别人来指手画脚.
 */
@Slf4j
public class TaskUserDefinition
{

    public static String status_pause = "pauseTaskProcessorAllocationStrategy";
    public static String status_resume = "resumeTaskProcessorAllocationStrategy";

    @Group("基本信息")
    @SerializedName("taskId")
    @Setter
    @Getter
    private String taskId;//便于全局分析问题,这里命名不是id,而是scheduleTaskId

    @SerializedName("beanName")
    @Setter
    @Getter
    private String beanName;

    @SerializedName("planRunStartTime")
    @Setter
    @Getter
    private String planRunStartTimeString;

    @SerializedName("planRunEndTime")
    @Setter
    @Getter
    private String planRunEndTimeString;


    @SerializedName("heartBeatRate")
    @Setter
    @Getter
    private long heartBeatRate = 5 * 1000;//心跳频率(秒)

    @SerializedName("judgeDeadInterval")
    @Setter
    @Getter
    private long judgeDeadInterval = 1 * 60 * 1000;//死亡间隔(秒)
    @SerializedName("expireOwnSignInterval")
    @Setter
    @Getter
    private double expireOwnSignInterval = 1;//清除处理域信息时长(小时)
    /**
     * pauseTaskProcessorAllocationStrategy,resumeTaskProcessorAllocationStrategy
     */
    @SerializedName("status")
    @Setter
    @Getter
    private String status = status_resume;
    @SerializedName("version")
    @Setter
    @Getter
    private long version;


    @Group("处理数据的情况")
    @SerializedName("processorType")
    @Setter
    @Getter
    private String processorType = "";
    @SerializedName("threadNumber")
    @Setter
    @Getter
    private int threadNumber = 5;

    @SerializedName("fetchNumber")
    @Setter
    @Getter
    private int fetchNumber = 500;//每次获取数据量

    @SerializedName("sleepTimeIntervalBeforeFetchTasks")
    @Setter
    @Getter
    private int sleepTimeIntervalBeforeFetchTasks = 0;//每次处理完数据后休眠时间(秒)
    @SerializedName("sleepTimeWhenNotHasData")
    @Setter
    @Getter
    private int sleepTimeWhenNotHasData = 500;//没有数据时休眠时长(秒)
    @SerializedName("maxExecuteNumber")
    @Setter
    @Getter
    private int maxExecuteNumber = 1;//每次执行数量  一般maxExecuteNumber<<fetchNumber.这里设置为1,用户可以自己修改(我这便于测试),一般不建议设置为0,一下全部处理太大了.


    @Group("任务项信息")
    @SerializedName("taskItemStrings")
    @Setter
    @Getter
    private String[] taskItemStrings;//是任务项不是任务数据
    @SerializedName("maxTaskItemsOfOneThreadGroup")
    @Setter
    @Getter
    private int maxTaskItemsOfOneThreadGroup = 0;
    @SerializedName("taskParameter")
    @Setter
    @Getter
    private String taskParameter;


    public void setPlanRunStartTimeString(String planRunStartTimeString)
    {
        this.planRunStartTimeString = planRunStartTimeString;
        if (this.planRunStartTimeString != null && this.planRunStartTimeString.trim().length() == 0)
        {
            this.planRunStartTimeString = null;
        }
    }

    public void setPlanRunEndTimeString(String planRunEndTimeString)
    {
        this.planRunEndTimeString = planRunEndTimeString;
        if (this.planRunEndTimeString != null && this.planRunEndTimeString.trim().length() == 0)
        {
            this.planRunEndTimeString = null;
        }

    }


    //0:{TYPE=A,KIND=1},1:{TYPE=A,KIND=2},2:{TYPE=A,KIND=3},3:{TYPE=A,KIND=4},4:{TYPE=A,KIND=5},5:{TYPE=A,KIND=6},6:{TYPE=A,KIND=7},7:{TYPE=A,KIND=8},8:{TYPE=A,KIND=9},9:{TYPE=A,KIND=10}
    public static String[] splitTaskItem(String string)
    {
        List<String> stringList = new ArrayList<String>();
        //
        int startIndex = 0;
        int endIndex = 0;
        //
        while (endIndex < string.length())
        {
            if (string.charAt(endIndex) == ':')
            {//:{},
                endIndex = string.indexOf('}', endIndex) + 1;//indexOf(int ch, int fromIndex),这里是,的index
                stringList.add(string.substring(startIndex, endIndex).trim());//substring(startIndex, endIndex) [)
                //
                while (endIndex < string.length())
                {
                    if (string.charAt(endIndex) == ' ')
                    {//如果是空格则跳出的是,   如果是,则直接跳出不再判断空格
                        endIndex = endIndex + 1;
                    } else
                    {
                        break;
                    }
                }
                endIndex = endIndex + 1; //跳过逗号
                startIndex = endIndex;
            } else if (string.charAt(endIndex) == ',')
            {//,
                stringList.add(string.substring(startIndex, endIndex).trim());
                //
                while (endIndex < string.length())
                {
                    if (string.charAt(endIndex) == ' ')
                    {//如果是空格则跳出的是,   如果是,则直接跳出不再判断空格
                        endIndex = endIndex + 1;
                    } else
                    {
                        break;
                    }
                }
                endIndex = endIndex + 1; //跳过逗号
                startIndex = endIndex;
            } else
            {
                //非最外层的:,直接略过
                endIndex = endIndex + 1;
            }
        }
        //最后一个endIndex不是:,的数据,还需要处理
        if (startIndex < string.length())
        {
            stringList.add(string.substring(startIndex).trim());
        }
        return (String[]) stringList.toArray(new String[0]);
    }


}
